package com.chuanjiabao.wisdom.ui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.adapter.SuccessorListAdapter;
import com.chuanjiabao.wisdom.bean.FamilyDeatilCallbackBean;
import com.chuanjiabao.wisdom.databinding.ActivitySuccessorListBinding;
import com.chuanjiabao.wisdom.listener.OnItemClickListener;
import com.chuanjiabao.wisdom.ui.BaseActivity;
import com.chuanjiabao.wisdom.ui.view.TaoLinear;
import com.chuanjiabao.wisdom.util.GsonUtil;
import com.chuanjiabao.wisdom.util.HTTPAPI;
import com.chuanjiabao.wisdom.util.SpResourceUtil;
import com.chuanjiabao.wisdom.util.StringUtil;
import com.chuanjiabao.wisdom.util.ToastUtil;
import com.lzy.okgo.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

public class SuccessorListActivity extends BaseActivity {

    private ActivitySuccessorListBinding binding;
    private SuccessorListAdapter listAdapter;
    private List<FamilyDeatilCallbackBean.DataBean.MembersBean> names = new ArrayList<>();
    private FamilyDeatilCallbackBean.DataBean.MembersBean mItem;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_successor_list);
        binding.setActivity(this);
        initView();
        getList();
    }

    private void initView() {
        setBack();
        setTitleText("选择传承人");
        setRightText("确定");
        setRightTextDraw(R.drawable.module_draw_success_ok);

        listAdapter = new SuccessorListAdapter(this, names);
        TaoLinear taoLinear = new TaoLinear(this, 4);
        taoLinear.setScrollEnabled(true);
        binding.recycler.setLayoutManager(taoLinear);
        binding.recycler.setAdapter(listAdapter);
        listAdapter.setOnItemClickLitener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                listAdapter.setOnSelectClickListener(position);
                mItem = names.get(position);
            }
        });

    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.rightText) {
            if (mItem == null) {
                ToastUtil.showToast("请选择传承人");
                return;
            }
            Intent intent = new Intent(SuccessorListActivity.this, SuccessorSelectActivity.class);
            intent.putExtra("module_bean", mItem);
            setResult(RESULT_OK, intent);
            finish();
        }
    }

    private void getList() {
        showProgressDialog();
        HTTPAPI.getInstance().getInheritList(
                getIntent().getStringExtra("id"),
                new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        cancelProgressDialog();
                        if (!StringUtil.isFail(s)) {
                            StringUtil.showFail(SuccessorListActivity.this, s);
                        } else {
                            try {
                                FamilyDeatilCallbackBean callbackBean = GsonUtil.GsonToBean(s, FamilyDeatilCallbackBean.class);
                                for (FamilyDeatilCallbackBean.DataBean dataBean : callbackBean.getData()) {
                                    for (FamilyDeatilCallbackBean.DataBean.MembersBean membersBean : dataBean.getMembers()) {
                                        if (membersBean.getD_user_id() != 0
                                                && !TextUtils.equals(SpResourceUtil.getUserId(), membersBean.getD_user_id() + "")) {
                                            names.add(membersBean);
                                        }
                                    }
                                }
                                listAdapter.notifyDataSetChanged();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
        );
    }

}
